#include <mqueue.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>



int main(){
    mqd_t mq=mq_open("/mq",O_CREAT | O_RDONLY,0666,NULL);
    if(mq==(mqd_t)-1){
        perror("mq_open error");
        exit(1);
    }
    char buffer[1024*1024];
    unsigned int prio=3;
    //mq_notify() 服务端可以通过该函数来注册一个异步通知方法，当有消息到来时，内核会调用该方法来通知服务端
    //mq_notify(mqd_t mqdes,const struct sigevent *notification)
    while(1){
        memset(buffer,0,sizeof(buffer));
        ssize_t len=mq_receive(mq,buffer,sizeof(buffer)-1,&prio);
        if(len<0){
            perror("mq_receive error");
            exit(2);
        }
        if(strncmp(buffer,"quit",4)==0){
            break;
        }
        printf("receive message:%s",buffer);
        fflush(stdout);
    }
    mq_close(mq);
    mq_unlink("/mq");
    return 0;
}